//
//  YWAnchorVoiceTableViewCell.m
//  聆听FM
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWAnchorVoiceTableViewCell.h"

@implementation YWAnchorVoiceTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backView = [[UIView alloc] init];
        [self.contentView addSubview:_backView];
        
        self.photoImageView = [[UIImageView alloc] init];
        [self.backView addSubview:_photoImageView];
        
        self.titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_titleLabel];
        
        self.iconImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_iconImageView];

        self.nickNameLabel = [[UILabel alloc] init];
        _nickNameLabel.font = [UIFont systemFontOfSize:13];
        _nickNameLabel.textColor = [UIColor colorWithWhite:0.603 alpha:1.000];
        [self.contentView addSubview:_nickNameLabel];
        
        self.playImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_playImageView];
        
        self.playCountLabel = [[UILabel alloc] init];
        _playCountLabel.font = [UIFont systemFontOfSize:13];
        _playCountLabel.textColor = [UIColor colorWithWhite:0.603 alpha:1.000];
        [self.contentView addSubview:_playCountLabel];
        
        self.clockImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_clockImageView];
        
        self.timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        _timeLabel.textColor = [UIColor colorWithWhite:0.603 alpha:1.000];
        [self.contentView addSubview:_timeLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        _dateLabel.font = [UIFont systemFontOfSize:13];
        _dateLabel.textColor = [UIColor colorWithWhite:0.603 alpha:1.000];
        _dateLabel.textAlignment = 2;
        [self.contentView addSubview:_dateLabel];
        
        self.lineLabel = [[UILabel alloc] init];
        _lineLabel.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_lineLabel];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _backView.frame = CGRectMake(15 * FITWIDTH, 10 * FITHEIGHT, 40 * FITWIDTH, 40 * FITWIDTH);
    _backView.layer.cornerRadius = 20;
    _photoImageView.frame = CGRectMake(2 * FITWIDTH, 2 * FITWIDTH, 36 * FITWIDTH, 36 * FITWIDTH);
    _photoImageView.layer.cornerRadius = 18;
    _photoImageView.layer.masksToBounds = YES;
    _titleLabel.frame = CGRectMake(65 * FITWIDTH, 5 * FITHEIGHT, 230 * FITWIDTH, 20 * FITHEIGHT);
    if ([_voiceModel.title length] > 17) {
        _titleLabel.frame = CGRectMake(65 * FITWIDTH, 5 * FITHEIGHT, 230 * FITWIDTH, 40 * FITHEIGHT);
        _titleLabel.numberOfLines = 2;
    }
    if ([_searchModel.title length] > 17) {
        _titleLabel.frame = CGRectMake(65 * FITWIDTH, 5 * FITHEIGHT, 230 * FITWIDTH, 40 * FITHEIGHT);
        _titleLabel.numberOfLines = 2;
    }
    _iconImageView.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.bounds.size.height + 14 * FITHEIGHT, 15 * FITWIDTH, 11 * FITWIDTH);
    _nickNameLabel.frame = CGRectMake(_titleLabel.frame.origin.x + 20 * FITWIDTH, _titleLabel.frame.origin.y + _titleLabel.bounds.size.height + 10 * FITHEIGHT, 200 * FITWIDTH, 20 * FITHEIGHT);
    _playImageView.frame = CGRectMake(_iconImageView.frame.origin.x, _iconImageView.frame.origin.y + _iconImageView.bounds.size.height + 11 * FITHEIGHT, 14 * FITWIDTH, 14 * FITWIDTH);
    _playCountLabel.frame = CGRectMake(_nickNameLabel.frame.origin.x , _nickNameLabel.frame.origin.y + _nickNameLabel.bounds.size.height + 5 * FITHEIGHT, 500 * FITWIDTH, 20 * FITHEIGHT);
    [_playCountLabel sizeToFit];
    _clockImageView.frame = CGRectMake(_playImageView.frame.origin.x + _playImageView.bounds.size.width + _playCountLabel.bounds.size.width + 20 * FITWIDTH, _playImageView.frame.origin.y, 13 * FITWIDTH, 13 * FITWIDTH);
    _timeLabel.frame = CGRectMake(_playCountLabel.frame.origin.x + _playCountLabel.bounds.size.width + _clockImageView.bounds.size.width + 20 * FITWIDTH, _playCountLabel.frame.origin.y, 500 * FITWIDTH, 20 * FITHEIGHT);
    [_timeLabel sizeToFit];
    _dateLabel.frame = CGRectMake(SCREENWIDTH - 110 * FITWIDTH, _titleLabel.frame.origin.y, 100 * FITWIDTH, 20 * FITWIDTH);
    _lineLabel.frame = CGRectMake(100, self.bounds.size.height - 1, SCREENWIDTH - 100, 1);
}

- (void)setVoiceModel:(YWAnchorVoiceModel *)voiceModel
{
    if (_voiceModel != voiceModel) {
        _voiceModel = voiceModel;
        [_photoImageView sd_setImageWithURL:[NSURL URLWithString:voiceModel.coverLarge] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
        _titleLabel.text = voiceModel.title;
        _iconImageView.image = [UIImage imageNamed:@"双人像"];
        _nickNameLabel.text = voiceModel.nickname;
        _playImageView.image = [UIImage imageNamed:@"淡颜色播放"];
        NSInteger playNum = voiceModel.playtimes;
        if (playNum < 10000) {
            _playCountLabel.text = [NSString stringWithFormat:@"%ld", playNum];
        }
        else
        {
            if (playNum % 10000 == 0) {
                _playCountLabel.text = [NSString stringWithFormat:@"%ld万", playNum / 10000];
            }
            else
            {
                _playCountLabel.text = [NSString stringWithFormat:@"%.1f万", playNum / 10000.0];
            }
        }
        _clockImageView.image = [UIImage imageNamed:@"表图"];
        NSInteger timeNum = voiceModel.duration;
        NSInteger minNum = timeNum / 60;
        NSInteger secNum = timeNum % 60;
        if (minNum < 10) {
            if (secNum < 10) {
                _timeLabel.text = [NSString stringWithFormat:@"0%ld:0%ld", minNum, secNum];
            }
            else
            {
                _timeLabel.text = [NSString stringWithFormat:@"0%ld:%ld", minNum, secNum];
            }
        }
        else
        {
            if (secNum < 10) {
                _timeLabel.text = [NSString stringWithFormat:@"%ld:0%ld", minNum, secNum];
            }
            else
            {
                _timeLabel.text = [NSString stringWithFormat:@"%ld:%ld", minNum, secNum];
            }
        }
        NSInteger createDate = voiceModel.createdAt / 1000;
        NSDate *time = [NSDate dateWithTimeIntervalSince1970:createDate];
        NSTimeInterval createTo1970 = [time timeIntervalSince1970];
        NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval nowTo1970 = [now timeIntervalSince1970];
        NSTimeInterval cha = nowTo1970 - createTo1970;
        NSString *timeString = @"";
        if (cha / 3600 < 1) {
            timeString = [NSString stringWithFormat:@"%f", cha / 60];
            timeString = [timeString substringToIndex:timeString.length - 7];
            timeString = [NSString stringWithFormat:@"%@分钟前", timeString];
        }
        if (cha / 3600 > 1 && cha / (3600 * 24) < 1) {
            timeString = [NSString stringWithFormat:@"%f", cha / 3600];
            timeString = [timeString substringToIndex:timeString.length - 7];
            timeString = [NSString stringWithFormat:@"%@小时前", timeString];
        }
        if (cha / (3600 * 24) > 1 && cha / (3600 * 24 * 365) < 1) {
            timeString = [NSString stringWithFormat:@"%f", cha / 86400];
            timeString = [timeString substringToIndex:timeString.length - 7];
            timeString = [NSString stringWithFormat:@"%@天前", timeString];
        }
        if (cha / (3600 * 24 * 365)) {
            //实例化一个NSDateFormatter对象
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            timeString = [[dateFormatter stringFromDate:time] substringWithRange:NSMakeRange(0, 7)];
        }
        _dateLabel.text = timeString;
    }
}

- (void)setSearchModel:(YWSearchModel *)searchModel
{
    if (_searchModel != searchModel) {
        _searchModel = searchModel;
        [_photoImageView sd_setImageWithURL:[NSURL URLWithString:searchModel.cover_path] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
        _titleLabel.text = searchModel.title;
        _iconImageView.image = [UIImage imageNamed:@"双人像"];
        _nickNameLabel.text = searchModel.nickname;
        _playImageView.image = [UIImage imageNamed:@"淡颜色播放"];
        NSInteger playNum = searchModel.count_play;
        if (playNum < 10000) {
            _playCountLabel.text = [NSString stringWithFormat:@"%ld", playNum];
        }
        else
        {
            if (playNum % 10000 == 0) {
                _playCountLabel.text = [NSString stringWithFormat:@"%ld万", playNum / 10000];
            }
            else
            {
                _playCountLabel.text = [NSString stringWithFormat:@"%.1f万", playNum / 10000.0];
            }
        }
        _clockImageView.image = [UIImage imageNamed:@"表图"];
        NSInteger timeNum = searchModel.duration;
        NSInteger minNum = timeNum / 60;
        NSInteger secNum = timeNum % 60;
        if (minNum < 10) {
            if (secNum < 10) {
                _timeLabel.text = [NSString stringWithFormat:@"0%ld:0%ld", minNum, secNum];
            }
            else
            {
                _timeLabel.text = [NSString stringWithFormat:@"0%ld:%ld", minNum, secNum];
            }
        }
        else
        {
            if (secNum < 10) {
                _timeLabel.text = [NSString stringWithFormat:@"%ld:0%ld", minNum, secNum];
            }
            else
            {
                _timeLabel.text = [NSString stringWithFormat:@"%ld:%ld", minNum, secNum];
            }
        }
        NSInteger createDate = searchModel.updated_at / 1000;
        NSDate *time = [NSDate dateWithTimeIntervalSince1970:createDate];
        NSTimeInterval createTo1970 = [time timeIntervalSince1970];
        NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval nowTo1970 = [now timeIntervalSince1970];
        NSTimeInterval cha = nowTo1970 - createTo1970;
        NSString *timeString = @"";
        if (cha / 3600 < 1) {
            timeString = [NSString stringWithFormat:@"%f", cha / 60];
            timeString = [timeString substringToIndex:timeString.length - 7];
            timeString = [NSString stringWithFormat:@"%@分钟前", timeString];
        }
        if (cha / 3600 > 1 && cha / (3600 * 24) < 1) {
            timeString = [NSString stringWithFormat:@"%f", cha / 3600];
            timeString = [timeString substringToIndex:timeString.length - 7];
            timeString = [NSString stringWithFormat:@"%@小时前", timeString];
        }
        if (cha / (3600 * 24) > 1 && cha / (3600 * 24 * 365) < 1) {
            timeString = [NSString stringWithFormat:@"%f", cha / 86400];
            timeString = [timeString substringToIndex:timeString.length - 7];
            timeString = [NSString stringWithFormat:@"%@天前", timeString];
        }
        if (cha / (3600 * 24 * 365)) {
            //实例化一个NSDateFormatter对象
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            timeString = [[dateFormatter stringFromDate:time] substringWithRange:NSMakeRange(0, 7)];
        }
        _dateLabel.text = timeString;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
